package com.huicoo.forestmanager.service;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;

import com.alibaba.sdk.android.push.MessageReceiver;
import com.alibaba.sdk.android.push.notification.CPushMessage;
import com.huicoo.forestmanager.CustomUtils;
import com.huicoo.forestmanager.network.bean.push.PushBean;
import com.huicoo.forestmanager.others.Constants;
import com.huicoo.forestmanager.ui.web.CustomWebActivity;
import com.huicoo.forestmanager.util.JsonUtils;
import com.huicoo.forestmanager.util.SharedPreferenceUtil;

import java.util.Map;

public class MyMessageReceiver extends MessageReceiver {
    // 消息接收部分的LOG_TAG
    public static final String REC_TAG = "receiver";
    @Override
    public void onNotification(Context context, String title, String summary, Map<String, String> extraMap) {
        Log.e("MyMessageReceiver", "Receive notification, title: " + title + ", summary: " + summary + ", extraMap: " + extraMap);
//        Toast.makeText(context,"Receive notification, title: " + title + ", summary: " + summary + ", extraMap: " + extraMap,Toast.LENGTH_LONG).show();
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//            NotificationManager mNotificationManager = (NotificationManager)context. getSystemService(Context.NOTIFICATION_SERVICE);
//            // 通知渠道的id
//            String id = "1";
//            // 用户可以看到的通知渠道的名字.
//            CharSequence name = title;
//            // 用户可以看到的通知渠道的描述
//            String description =summary;onNotificationOpened
//            int importance = NotificationManager.IMPORTANCE_HIGH;
//            NotificationChannel mChannel = new NotificationChannel(id, name, importance);
//            // 配置通知渠道的属性
//            mChannel.setDescription(description);
//            // 设置通知出现时的闪灯（如果 android 设备支持的话）
//            mChannel.enableLights(true);
//            mChannel.setLightColor(Color.RED);
//            // 设置通知出现时的震动（如果 android 设备支持的话）
//            mChannel.enableVibration(true);
//            mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
//            //最后在notificationmanager中创建该通知渠道
//            mNotificationManager.createNotificationChannel(mChannel);
//        }

    }
    @Override
    public void onMessage(Context context, CPushMessage cPushMessage) {
            Log.e("MyMessageReceiver", "onMessage, messageId: " + cPushMessage.getMessageId() + ", title: " + cPushMessage.getTitle() + ", content:" + cPushMessage.getContent());
    }

    //点击通知栏
    @Override
    public void onNotificationOpened(Context context, String title, String summary, String extraMap) {
        Log.e("MyMessageReceiver", "onNotificationOpened, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);

        if (TextUtils.isEmpty(extraMap)){
            return;
        }

        PushBean pushBean = JsonUtils.fromJson(extraMap, PushBean.class);
        if (pushBean==null && TextUtils.isEmpty(pushBean.getTagId())){
            return;
        }

        String baseUrl = SharedPreferenceUtil.LoadStringData(Constants.PROJECT_NAME, Constants.BASE_URL);
        String url = CustomUtils.urlAddParams(context, baseUrl + "h5/eventHandleDetail.html") + "&id=" + pushBean.getTagId();
        Intent intent = new Intent(context, CustomWebActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("url", url);
        intent.putExtras(bundle);
        context.startActivity(intent);

    }
    @Override
    protected void onNotificationClickedWithNoAction(Context context, String title, String summary, String extraMap) {
        Log.e("MyMessageReceiver", "onNotificationClickedWithNoAction, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap);
    }
    @Override
    protected void onNotificationReceivedInApp(Context context, String title, String summary, Map<String, String> extraMap, int openType, String openActivity, String openUrl) {
        Log.e("MyMessageReceiver", "onNotificationReceivedInApp, title: " + title + ", summary: " + summary + ", extraMap:" + extraMap + ", openType:" + openType + ", openActivity:" + openActivity + ", openUrl:" + openUrl);
    }
    @Override
    protected void onNotificationRemoved(Context context, String messageId) {
        Log.e("MyMessageReceiver", "onNotificationRemoved");
    }


}